
package gui;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import lejos.pc.comm.NXTConnector;

/**
 *
 * @author  Beto
 */
public class NxtEcompApp extends javax.swing.JFrame {
    
    private MainPanel mainPanel;
    private boolean conectado = false;
    private DataOutputStream dos;
    private DataInputStream dis;
    
    public NxtEcompApp() {
        initComponents();
        
        mainPanel = new MainPanel() {
            void movimentarRobo(double x, double y) {
                if(conectado) {
                    try {
                        dos.writeDouble(x);
                        dos.writeDouble(y);
                        dos.flush();
                    }
                    catch (IOException ioe) {
                        System.out.println("IO Exception writing bytes:");
                        System.out.println(ioe.getMessage());
                    }
                }
                else
                    JOptionPane.showMessageDialog(null, "Robô não está conectado!",
                        "Erro", JOptionPane.ERROR_MESSAGE);
            }

            void mouseClicado() {
                if(!grafosButton.isEnabled()) grafosButton.setEnabled(true);
                if(!reiniciarGrafo.isEnabled()) reiniciarGrafo.setEnabled(true);
            }
        };        
        
        jPanel1.add(mainPanel);        
        this.setLocation(20, 0);
        pack();

    }    
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        conectarButton = new javax.swing.JButton();
        grafosButton = new javax.swing.JButton();
        reiniciarGrafo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("..:: NXT Ecomp ::..");
        setResizable(false);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(new java.awt.BorderLayout());

        jToolBar1.setRollover(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(13, 30));

        conectarButton.setText("Conectar");
        conectarButton.setFocusable(false);
        conectarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        conectarButton.setMinimumSize(new java.awt.Dimension(51, 25));
        conectarButton.setPreferredSize(new java.awt.Dimension(51, 30));
        conectarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        conectarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conectarButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(conectarButton);

        grafosButton.setText("Gerar Grafos");
        grafosButton.setEnabled(false);
        grafosButton.setFocusable(false);
        grafosButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        grafosButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        grafosButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                grafosButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(grafosButton);

        reiniciarGrafo.setText("Reiniciar Grafo");
        reiniciarGrafo.setEnabled(false);
        reiniciarGrafo.setFocusable(false);
        reiniciarGrafo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        reiniciarGrafo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        reiniciarGrafo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reiniciarGrafoActionPerformed(evt);
            }
        });
        jToolBar1.add(reiniciarGrafo);

        jPanel3.add(jToolBar1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel3, java.awt.BorderLayout.NORTH);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setPreferredSize(new java.awt.Dimension(100, 25));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 396, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 21, Short.MAX_VALUE)
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void conectarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conectarButtonActionPerformed
        if(!conectado) {
            NXTConnector conn = new NXTConnector();
            conectado = conn.connectTo("btspp://");

            if (!conectado)
                JOptionPane.showMessageDialog(null, "Não foi possível conectar!",
                        "Erro ao conectar", JOptionPane.ERROR_MESSAGE);
            else {
                JOptionPane.showMessageDialog(null, "Conectado ao NXT!",
                        "Conectado", JOptionPane.INFORMATION_MESSAGE);

                dos = conn.getDataOut();
                dis = conn.getDataIn();
            }
        }
    }//GEN-LAST:event_conectarButtonActionPerformed

    private void grafosButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_grafosButtonActionPerformed
        mainPanel.gerarGrafos();
    }//GEN-LAST:event_grafosButtonActionPerformed

    private void reiniciarGrafoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reiniciarGrafoActionPerformed
        // TODO add your handling code here:
        mainPanel.PontoInicial();
    }//GEN-LAST:event_reiniciarGrafoActionPerformed
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton conectarButton;
    private javax.swing.JButton grafosButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton reiniciarGrafo;
    // End of variables declaration//GEN-END:variables
    
}
